﻿using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using MessageBox = System.Windows.MessageBox;

namespace ZhuXingFamilys.test
{
    [Transaction(TransactionMode.Manual)]
    public class FamilyFIleConvert : IExternalEventHandler
    {
        public string RfaPath { set; get; }
        public string Categroyname { set; get; }
        public string Familynumber { set; get; }
        public string Unit { get; set; }
        public string Projectname { set; get; }
        public string Projectfeatures { get; set; }
        public int Length { get; set; }
        public int Height { get; set; }

        public void Execute(UIApplication app)
        {
            try
            {
                //UIApplication uiApp = app.Application;
                //Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;
                Document newDoc = app.Application.OpenDocumentFile(RfaPath);

                FamilyManager familyManager = newDoc.FamilyManager;


                if (newDoc != null)
                {
                    //切换族类型
                    using (Transaction trans = new Transaction(newDoc, "changeType"))
                    {
                        trans.Start();
                        //newDoc.FamilyManager.NewType(Categroyname);
                        
                        foreach (FamilyType familyType in familyManager.Types)
                        {
                            if (familyType.Name == "基础梁-120x200-C20")
                            {
                                familyManager.CurrentType = familyType;
                                //familyType.Name.Replace("基础梁-120x200-C20", "C20000");
                                newDoc.FamilyManager.RenameCurrentType(Categroyname);
                                
                            }
                            //if (familyType.Name == Categroyname)
                            //{
                            //    //familyType.Name.Replace("基础梁-120x200-C20", "C20000");
                            //    familyManager.CurrentType = familyType;
                            //    //break;
                            //}

                        }
                        trans.Commit();
                    }
                    //获取族参数
                    //FamilyParameter fParameter = familyManager.get_Parameter("族库编号");
                    //获取参数值
                    //double fValue = familyManager.CurrentType.AsDouble(fParameter).Value;
                    ////获取参数公式
                    //string formula = fParameter.Formula;

                    //FamilyParameter paramAdded = null;
                    using (Transaction trans = new Transaction(newDoc))
                    {
                        trans.Start("Update Parameter");
                        //FamilyParameter fParametercn = familyManager.get_Parameter("类型名称");
                        //familyManager.Set(fParametercn, categroyname);
                        FamilyParameter fParameterfn = familyManager.get_Parameter("族库编号");
                        familyManager.Set(fParameterfn, Familynumber);
                        FamilyParameter fParameterunit = familyManager.get_Parameter("计量单位");
                        familyManager.Set(fParameterunit, Unit);
                        FamilyParameter fParameterpn = familyManager.get_Parameter("项目名称");
                        familyManager.Set(fParameterpn, Projectname);
                        FamilyParameter fParameterpf = familyManager.get_Parameter("项目特征");
                        familyManager.Set(fParameterpf, Projectfeatures);
                        FamilyParameter fParameterb = familyManager.get_Parameter("b");
                        familyManager.Set(fParameterb, Length/304.8);
                        FamilyParameter fParameterh = familyManager.get_Parameter("h");
                        familyManager.Set(fParameterh, Height/304.8);
                        //paramAdded = familyManager.AddParameter("自定义参数1", BuiltInParameterGroup.PG_TEXT, ParameterType.Text, false);
                        //double fValue = familyManager.CurrentType.AsDouble(a).Value;
                        //string paraName = "NewParam4";
                        //BuiltInParameterGroup paraGroup = BuiltInParameterGroup.PG_TEXT;
                        //ParameterType paraType = ParameterType.Text;

                        //familyManager.AddParameter(paraName, paraGroup, paraType, false);
                        trans.Commit();
                    }
                    //if (paramAdded == null)
                    //{
                    //    return Result.Failed;
                    //}
                    //using (Transaction trans = new Transaction(newDoc))
                    //{


                    //    trans.Start("Add Value");
                    //    familyManager.Set(paramAdded, "111");
                    //    trans.Commit();
                    //}

                }
                SaveOptions saveOpt = new SaveOptions();
                saveOpt.Compact = true;
                string repath = @"D:\" + Categroyname.Replace("*", "x") + ".rfa";
                newDoc.SaveAs(repath);
                newDoc.Close();
                //return Result.Succeeded;
            }
            catch (Exception exception)
            {
                //message = exception.Message;
                //return Result.Failed;
            }
        }

        public string GetName()
        {
            return "FamilyFIleConvert";
        }
    }
}
